package easy;

import java.util.HashSet;
import java.util.Set;

public class Solution_202 {

    public static boolean isHappy(int n) {
        if (n==0)
            return false;
        Set<Integer> set = new HashSet<>();
        set.add(n);
        int temp = 0;
        for (;;) {
            int mod = n%10;
            temp += mod*mod;
            n/=10;
            if (n==0) {
                if (temp==1) {
                    return true;
                } else {
                    if (set.contains(temp)) {
                        return false;
                    }
                    set.add(temp);
                    n = temp;
                    temp = 0;
                }
            }
        }
    }

    public static void main(String[] args) {
        int[] a = { };
        System.out.println(isHappy(20));
    }
}
